package com.majie.httpclient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 *
 * restTemplate的再次封装
 * 可以配置直连模式和基于服务模式
 * @author majie
 */
@Component
public class CustomRestTemplate {

    private final RestTemplate restTemplate;

    @Autowired
    public CustomRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }


    public <T> ResponseEntity<T> post(String url, Object reqBody, ParameterizedTypeReference<T> responseType) {

        return restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(reqBody), responseType);
    }

    public <T> ResponseEntity<T> get(String url, ParameterizedTypeReference<T> responseType) {

        return restTemplate.exchange(url, HttpMethod.GET, HttpEntity.EMPTY, responseType);
    }


}